﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using PropertyExpression.Common;

namespace PropertyExpression.ControllableQuery.Demo
{
    class Program
    {
        static void Main()
        {
            RazorInit.Register();

            using (var entities = new NorthwindEntities())
            {
                var customerses = entities.Customers;
                var i = 10;
                var goldCutomer = customerses.Where(_ => qwe(_));
                var list = goldCutomer.ToList();
            }

//            var list2 = Query<IOrder>.New(new {threshold = 10m}, @"
//SELECT  CustomerID
//FROM    Customers c
//WHERE   @It.GoldCutomer(threshold)
//").List();


        }

        private static bool qwe(Customers _)
        {
            return _.Address == "qe" && _.City == "qwe";
        }

        public static QFragment GoldCutomer(Param<decimal> threshold)
        {
            return new {threshold}.QFragment(@"
(SELECT SUM(Quantity * UnitPrice)
 FROM   [Order Details] od
        JOIN Orders o ON od.OrderID = o.OrderID
 WHERE  o.CustomerID = c2.CustomerID
) > @threshold");
        }
    }

    internal static class Util
    {

        public static IQueryable<Customers> GoldCutomer(this IQueryable<Customers> source, decimal threshold)
        {
            return source.Where(
                c => c.Orders.Sum(o => o.Order_Details.Sum(
                    line => line.Quantity*line.UnitPrice)) > threshold);
        }
    }

    internal interface IOrder
    {
        string CustomerID { get; }
    }
}
